<template>
  <el-form
    ref="ruleFormRef"
    :model="coForm"
    :rules="rules"
    class="demo-ruleForm"
    label-position="top"
    size="small"
  >
    <el-form-item
      :label="t('plc.acknowledgementRequired')"
      prop="acknowledgementRequired"
    >
      <el-switch v-model="coForm.acknowledgementRequired" />
    </el-form-item>
    <el-form-item
      :label="t('plc.com')"
      prop="payload.com"
      placeholder="组件编号"
    >
      <el-input
        v-model="coForm.payload.com"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item
      :label="t('plc.car')"
      prop="payload.car"
      placeholder="载体编号"
    >
      <el-input
        v-model="coForm.payload.car"
        @keyup.enter="submitForm(ruleFormRef)"
      />
    </el-form-item>
    <el-form-item
      :label="t('plc.dir')"
      prop="payload.dir"
      placeholder="方向码"
    >
      <el-input-number
        :min="0"
        v-model="coForm.payload.dir"
      />
    </el-form-item>
    <el-form-item
      :label="t('plc.sta')"
      prop="payload.sta"
    >
      <el-select
        v-model.number="coForm.payload.sta"
        placeholder="状态码"
      >
        <el-option
          v-for="(item, index) in options"
          :key="index"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>

    <!-- <el-form-item
      :label="t('plc.dcl')"
      prop="payload.dcl"
    >
      <el-checkbox-group v-model="coForm.payload.dcl">
        <el-checkbox
          v-for="i in 5"
          :key="i"
          :label="i"
        />
      </el-checkbox-group>
    </el-form-item>
    <el-form-item
      :label="t('plc.scl')"
      prop="payload.scl"
    >
      <el-checkbox-group v-model="coForm.payload.scl">
        <el-checkbox
          v-for="i in 5"
          :key="i"
          :label="i"
        />
      </el-checkbox-group>
    </el-form-item> -->
    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm(ruleFormRef)"
        :disabled="!plcServerStatus"
        :loading="loading"
      >
        {{ t('plc.send') }}
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">
        {{ t('plc.reset') }}
      </el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
import { useI18n } from 'vue-i18n'
import { sendMessageByServer } from '@/api/plc'
import { plcStore } from '@/store/plcStore'
import { storeToRefs } from 'pinia'

const store = plcStore()
const loading = ref(false)

const { plcServerStatus, plcServerPort } = storeToRefs(store) // 连接状态
const { t } = useI18n()
const ruleFormRef = ref<FormInstance>()

const props = defineProps({
  ip: {
    type: String,
    required: false,
    default: '127.0.0.1'
  },
  port: {
    type: Number,
    required: false,
    default: 2000
  }
})

const coForm = reactive({
  acknowledgementRequired: false,
  payloadType: 'CONV',
  payload: {
    com: '',
    evt: 'CO',
    car: '',
    dir: 0,
    sta: 0,
    // dcl: [] as number[],
    // scl: [] as number[]
  }
})

const rules = reactive<FormRules>({
  'payload.car': [
    {
      required: true,
      message: '请填写载体编号',
      trigger: 'blur'
    }
  ],
  'payload.com': [
    {
      required: true,
      message: '请填写组件编号',
      trigger: 'blur'
    }
  ],
  'payload.dir': [
    {
      required: true,
      message: '请填写方向码',
      trigger: 'change'
    },
    { type: 'number', message: '方向码必须为数字类型' }
  ],
  'payload.sta': [
    {
      required: true,
      message: '请填写状态码',
      trigger: 'change'
    }
  ],
  // 'payload.dcl': [
  //   {
  //     type: 'array',
  //     required: false
  //   }
  // ],
  // 'payload.scl': [
  //   {
  //     type: 'array',
  //     required: false
  //   }
  // ]
})

const options = reactive([
  {
    label: 'Default/Unknown',
    value: -1
  },
  {
    label: 'Position OK',
    value: 0
  },
  {
    label: 'Position Full',
    value: 1
  },
  {
    label: 'Position Error',
    value: 2
  },
  {
    label: 'Destination Too Late',
    value: 3
  },
  {
    label: 'Hardware Fail',
    value: 4
  },
  {
    label: 'Hardware Reset',
    value: 5
  },
  {
    label: 'Tracking Error',
    value: 6
  }
])

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      loading.value = true
      sendMessageByServer(coForm).then(res => {
        ElMessage({
          showClose: true,
          message: res.data.message,
          type: 'success',
          duration: 1000
        })
      }).finally(() => {
        loading.value = false
      })
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>
